<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use app\admin\model\Banner as BannerModel;

class Banner extends Base
{
//  public function lst()
//  {
//      $banner = new BannerModel();
//      $banners = $banner->paginate(10);
//      $this->assign('banners',$banners);
//      return view();
//  }



    
    
	public function lst()
    {
        $banner = new BannerModel();
        if(request()->isPost()){
            $sorts = input('post.');
            foreach ($sorts as $k => $v) {
                $banner->update(['id'=>$k,'sort'=>$v]);
            }
            $this->success('更新排序成功！',url('lst'));
            return;
        }
        $banners = $banner->order('sort asc')->paginate(5);
        $this->assign('banners',$banners);
        return view();
    }

    public function add()
    {
        if(request()->isPost()){
            $data = input('post.');

            $validate = \think\Loader::validate('Banner');
            if(!$validate->scene('add')->check($data)){
                $this->error($validate->getError());
            }
            $banner = new BannerModel();

            if($banner->save($data)){
                $this->success('添加轮播图成功',url('lst'));
            }else{
                $this->error('添加轮播图失败！');
            }
            return;
        }
        return view();
    }

    public function edit(){
        $id = input('id');
        $banner = new BannerModel();
        if (request()->isPost()){
            $data = input('post.');
            $validate = \think\Loader::validate('Banner');
            if(!$validate->scene('edit')->check($data)){
                $this->error($validate->getError());
                die;
            }
            $data = [
                'id'       =>input('id'),
                'title'    =>input('title'),
                'sort'     =>input('sort'),
                'rec'      =>input('rec'),
                'ispc'      =>input('ispc'),
                'url'      =>input('url'),
            ];
            $save = $banner->update($data);
            if($save){
                $this->success('修改轮播图成功','lst');
            }else{
                $this->error('修改轮播图失败');
            }
            return;
        }
        $banners = $banner->find($id);
        $this->assign('banners', $banners);
        return view();
    }

    public function del(){
        $id = input('id');
        if(BannerModel::destroy($id)){
            $this->success("删除轮播图成功！", 'lst');
        }else{
            $this->error('删除轮播图失败');
        }
    }
}
